﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.Generic;
namespace WPFExcelReport
{

    public partial class VBPRULES : ViewBase
    {
        #region ==IsZutei===========
        public static readonly DependencyProperty IsZuteiProperty = DependencyProperty.Register("IsZutei",
        typeof(bool), typeof(VBPRULES),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsZuteiChanged));
        public bool IsZutei
        {
            get
            {
                return (bool)GetValue(IsZuteiProperty);
            }
            set
            {
                SetValue(IsZuteiProperty, value);
            }
        }
        private static void OnIsZuteiChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.IsZutei = (bool)e.NewValue;
            }
        }
        #endregion

        public VICC_PRULES TMV
        {
            get
            {
                return (VICC_PRULES)MV;
            }
        }
        public VBPRULES()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_PRULES();
            this.DataContext = MV;
            MESSAGES += "RELOADRULE;";
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public void LOAD()
        {
            SResult = TMV.QueryICC_PRULES(null, 9999);
            MergeData(SResult, false);
            MV.TBTemp = TBMain.Copy();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwRule = dwMaster;
            R01 = TMV.R01;
            P01 = TMV.P1;
        } 
        public void LOADBYAUF(string _order)
        {
            TBMain.Clear();
            if (_order == null)
                return;
            if (_order.Length < 3)
                return;
            KRS = _order.Substring(0, 2);
            LFD = _order.Substring(2, 6);
            POS = _order.Substring(8, 3);
            PM["ORDERNUM"] = _order;
            TMV.LOAD(PM);
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void LOADBYAUF()
        {
            LOADBYAUF(ORDERNUM);
            base.LOADBYAUF();
        }

        public void Insert(DataRow _dwRule)
        {
            if (_dwRule == null)
                return;
            TMV.InsertICC_PRULES(_dwRule, app.company);
            TBMain.Rows.InsertAt(_dwRule, 0);
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void Save()
        {
            if (dwMaster == null)
                return;
            TMV.UpdateICC_PRULES(dwMaster, 0);
        }

        public void Locate()
        {
            if (vwMaster.View == null)
                return;
            vwMaster.View.MoveCurrentTo(-1);
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["ARTSTAGE"].ToString().Trim() == ARTSTAGE.Trim()
                        select row;
            foreach (DataRow row in query)
            {
                int idx = TBMain.Rows.IndexOf(row);
                vwMaster.View.MoveCurrentToPosition(idx);
                break;
            }
        }

        public virtual void Reset()
        {
            filter = "ARTSTAGE='" + ARTSTAGE + "'";
            rows = MV.TBTemp.Select(filter);
            foreach (DataRow row in rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            Save();
            base.SaveCommand(sender, e);
        }

        public virtual void TextBox_KeyDown(object sender, KeyEventArgs e)
        {

        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DPRULES01(dwMaster);
            base.DeleteCommand(sender, e);
        }

        public void Rules_KeyUp(object sender, KeyEventArgs e)
        {
            
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            this.Focus();
            TextBox tb = sender as TextBox; 
            tb.Focus(); 
        }
    }
    public partial class VBPRULES
    {

        #region ==FLOW===========
        public static readonly DependencyProperty FLOWProperty = DependencyProperty.Register("FLOW",
        typeof(string), typeof(VBPRULES),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFLOWChanged));
        public string FLOW
        {
            get
            {
                return (string)GetValue(FLOWProperty); 
            }
            set
            {
                SetValue(FLOWProperty, value);
               
            }
        }
        private static void OnFLOWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.FLOW = (string)e.NewValue;
                src.OnFLOWChg((string)e.NewValue);
            }
        }
        public virtual void OnFLOWChg(string value)
        {
            if (dwMaster == null)
                return;
            if (value == "")
                return;
            TMV.UNDES = value;
        }
        #endregion
        #region ==dwRule===========
        public static readonly DependencyProperty dwRuleProperty = DependencyProperty.Register("dwRule",
        typeof(DataRow), typeof(VBPRULES),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwRuleChanged));
        public DataRow dwRule
        {
            get
            {
                return (DataRow)GetValue(dwRuleProperty);
            }
            set
            {
                SetValue(dwRuleProperty, value);
               
            }
        }
        private static void OdwRuleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwRule = row;
                src.OnRuleChg(row);
            }
        }
        public virtual void OnRuleChg(DataRow rule)
        {
            if (MV == null) return;
            TMV.dwRule = rule;
        }
        #endregion

        #region ==dwVKK===========
        public static readonly DependencyProperty dwVKKProperty = DependencyProperty.Register("dwVKK",
        typeof(DataRow), typeof(VBPRULES),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwVKKChanged));
        public DataRow dwVKK
        {
            get
            {
                return (DataRow)GetValue(dwVKKProperty);
            }
            set
            {
                SetValue(dwVKKProperty, value);
                Reset();
            }
        }
        private static void OdwVKKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwVKK = row;
            }
        }
        #endregion

        #region ==dwAP===========
        public static readonly DependencyProperty dwAPProperty = DependencyProperty.Register("dwAP",
        typeof(DataRow), typeof(VBPRULES),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwAPChanged));
        public DataRow dwAP
        {
            get
            {
                return (DataRow)GetValue(dwAPProperty);
            }
            set
            {
                SetValue(dwAPProperty, value);
               
            }
        }
        private static void OndwAPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.dwAP = (DataRow)e.NewValue;
                src.OnAPModeChg((DataRow)e.NewValue);
            }
        }
        public virtual void OnAPModeChg(DataRow row)
        {
            TMV.dwAP = row;
            if (row != null)
            {
                TMV.APZANRKRS = row["APKANRKRS"].ToString();
                TMV.APZALFDNR = row["APKALFDNR"].ToString();
                TMV.UNDES = row["APKANRKRS"].ToString() + row["APKALFDNR"].ToString();
                TMV.P8 = row["INMODE"].ToString();
                TMV.P9 = row["OUTMODE"].ToString();
            } 
        }
        #endregion

        #region ==ARTSTAGE===========
        public static readonly DependencyProperty ARTSTAGEProperty = DependencyProperty.Register("ARTSTAGE",
        typeof(string), typeof(VBPRULES),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnARTSTAGEChanged));
        public string ARTSTAGE
        {
            get
            {
                if (MV == null) return "";
                return TMV.ARTSTAGE;
            }
            set
            {
                SetValue(ARTSTAGEProperty, value);
                if (MV == null) return;
                TMV.ARTSTAGE = value;
            }
        }
        private static void OnARTSTAGEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.ARTSTAGE = (string)e.NewValue;
            }
        }
        #endregion


        #region ==RLPRART===========
        public static readonly DependencyProperty RLPRARTProperty = DependencyProperty.Register("RLPRART",
        typeof(string), typeof(VBPRULES),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRLPRARTChanged));
        public string RLPRART
        {
            get
            {
                return (string)GetValue(RLPRARTProperty);
            }
            set
            {
                SetValue(RLPRARTProperty, value);
               
              
            }
        }
        private static void OnRLPRARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.RLPRART = (string)e.NewValue;
                src.OnRLPRARTChg();
            }
        }
        public virtual void OnRLPRARTChg()
        { }
        #endregion


        #region ==R01===========
        public static readonly DependencyProperty R01Property = DependencyProperty.Register("R01",
        typeof(string), typeof(VBPRULES),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnR01Changed));
        public string R01
        {
            get
            {
                return (string)GetValue(R01Property);
            }
            set
            {
                SetValue(R01Property, value);
                OnR01Chg();
            }
        }
        private static void OnR01Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.R01 = (string)e.NewValue;
            }
        }
        public virtual void OnR01Chg()
        {
            if (MV != null)
                TMV.R01 = R01;
        }
        #endregion

        #region ==P01===========
        public static readonly DependencyProperty P01Property = DependencyProperty.Register("P01",
        typeof(string), typeof(VBPRULES),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnP01Changed));
        public string P01
        {
            get
            {
                return (string)GetValue(P01Property);
            }
            set
            {
                SetValue(P01Property, value);
                if (MV == null) return;
                TMV.P1 = value;
            }
        }
        private static void OnP01Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VBPRULES;
            if (src != null)
            {
                src.P01 = (string)e.NewValue;
            }
        }
        #endregion

    }
}
